package orion.jarvis.task;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.google.appengine.api.datastore.Key;

/**
 * @author Ted
 * 
 */
@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Task {
	@PrimaryKey
	@Persistent
	private Long id;
	@Persistent
	private Category category;
	@Persistent
	private Desciption description;
	@Persistent
	private Actor assignment;
	@Persistent
	private Actor createBy;
	@Persistent
	private Date createDate;
	@Persistent
	private Date startDate;
	@Persistent
	private Date estFinishDate;
	@Persistent
	private Date actFinishDate;

	// @Persistent
	// private Actor[] subscribers;

	// @Persistent(defaultFetchGroup = "true",mappedBy = "id")
	// 
	// private Task[] subTasks;

	public Task() {
		super();
	}

	public Date getActFinishDate() {
		return actFinishDate;
	}

	public Actor getAssignment() {
		return assignment;
	}

	public Category getCategory() {
		return category;
	}

	public Actor getCreateBy() {
		return createBy;
	}

	public Date getCreateDate() {
		return createDate;
	}

	public Desciption getDescription() {
		return description;
	}

	public Date getEstFinishDate() {
		return estFinishDate;
	}

	public Long getId() {
		return id;
	}

	public Date getStartDate() {
		return startDate;
	}

	public void setActFinishDate(Date actFinishDate) {
		this.actFinishDate = actFinishDate;
	}

	public void setAssignment(Actor assignment) {
		this.assignment = assignment;
	}

	public void setCategory(Category category) {
		this.category = category;
	}

	public void setCreateBy(Actor createBy) {
		this.createBy = createBy;
	}

	public void setCreateDate(Date createDate) {
		this.createDate = createDate;
	}

	public void setDescription(Desciption description) {
		this.description = description;
	}

	public void setEstFinishDate(Date estFinishDate) {
		this.estFinishDate = estFinishDate;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public void setStartDate(Date startDate) {
		this.startDate = startDate;
	}

	public boolean isSameCategory(Task otherTask) {
		return this.category.isEqual(otherTask.getCategory());
	}

	public boolean isEqual(Task otherTask) {
		return this.id.equals(otherTask.getId());
	}

	// public Actor[] getSubscribers() {
	// return subscribers;
	// }
	//	
	// public void setSubscribers(Actor[] subscribers) {
	// this.subscribers = subscribers;
	// }

	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		sb.append("[");
		if (id != null)
			sb.append("id = ").append(id).append(", ");
		if (category != null)
			sb.append("category = ").append(category.getDesc()).append(", ");
		if (description != null)
			sb.append("description = ").append(description.getDescription()).append(", ");
		if (assignment != null)
			sb.append("assignment = ").append(assignment.toString()).append(", ");
		if (createBy != null)
			sb.append("createBy = ").append(createBy.toString()).append(", ");
		// if (subscribers != null && subscribers.length > 0)
		// sb.append("subscribers = ").append(Arrays.deepToString(subscribers)).append(", ");
		if (createBy != null)
			sb.append("createBy = ").append(createBy.toString()).append(", ");
		if (createDate != null)
			sb.append("createDate = ").append(df.format(createDate)).append(", ");
		if (startDate != null)
			sb.append("startDate = ").append(df.format(startDate)).append(", ");
		if (estFinishDate != null)
			sb.append("estFinishDate = ").append(df.format(estFinishDate)).append(", ");
		if (actFinishDate != null)
			sb.append("actFinishDate = ").append(df.format(actFinishDate));
		sb.append("]");
		return sb.toString();
	}
	//
	// /**
	// * @return the subTasks
	// */
	// public Task[] getSubTasks() {
	// return subTasks;
	// }
	//
	// /**
	// * @param subTasks
	// * the subTasks to set
	// */
	// public void setSubTasks(Task[] subTasks) {
	// this.subTasks = subTasks;
	// }
	//
	// public void addSubTask(Task subTask) {
	// if (this.subTasks == null)
	// this.subTasks = new Task[] {};
	// List<Task> newTaskList = Lists.newArrayList(subTasks);
	// newTaskList.add(subTask);
	// this.subTasks = newTaskList.toArray(new Task[]{});
	// }
	//
	// public void addSubTask(int index, Task subTask) {
	// if (this.subTasks == null)
	// this.subTasks = new Task[] {};
	// List<Task> newTaskList = Lists.newArrayList(subTasks);
	// newTaskList.add(index, subTask);
	// this.subTasks = newTaskList.toArray(new Task[]{});
	// }
	//
	// public void addSubTask(List<Task> subTasks) {
	// if (this.subTasks == null)
	// this.subTasks = new Task[] {};
	// List<Task> newTaskList = Lists.newArrayList(subTasks);
	// newTaskList.addAll(subTasks);
	// this.subTasks = newTaskList.toArray(new Task[]{});
	// }
	//
	// public void removeSubTask(Task subTask) {
	// if (this.subTasks == null)
	// this.subTasks = new Task[] {};
	// List<Task> newTaskList = Lists.newArrayList(subTasks);
	// newTaskList.remove(subTask);
	// this.subTasks = newTaskList.toArray(new Task[]{});
	// }
	//
	// public void removeSubTask(int index) {
	// if (this.subTasks == null)
	// this.subTasks = new Task[] {};
	// List<Task> newTaskList = Lists.newArrayList(subTasks);
	// newTaskList.remove(index);
	// this.subTasks = newTaskList.toArray(new Task[]{});
	// }

}
